from decimal import *
import  sys
import sys
import requests
import pymysql
import time
import datetime
import random
import sys
import tushare as ts
import pymysql
import requests
import time
import  os
#时间格式化方法
import datetime
import shutil
from sqlalchemy import create_engine
def  test1():
    print('start')
    s = requests.Session()
    res=s.get('http://localhost:9981/menu/list')
    #print(type(res.content))
    #print(res.content)
    ss=res.content
    print(ss.decode('utf-8'))
    #s='{"retCode":"0000","retMsg":"\xe7\x99\xbb\xe5\xbd\x95\xe6\x88\x90\xe5\x8a\x9f","retData":[{"id":"101","parentid":"0","name":"\xe7\xb3\xbb\xe7\xbb\x9f\xe8\xae\xbe\xe7\xbd\xae","path":"/title1","children":[{"id":"201","parentid":"101","name":"\xe6\x95\xb0\xe6\x8d\xae\xe7\xbb\x9f\xe8\xae\xa1","path":"/statistics","children":null},{"id":"202","parentid":"101","name":"\xe8\xb6\x8b\xe5\x8a\xbf\xe5\x88\x86\xe6\x9e\x90","path":"/trend","children":null},{"id":"203","parentid":"101","name":"\xe6\x95\xb0\xe6\x8d\xae\xe5\x88\x86\xe6\x9e\x90","path":"/range","children":null},{"id":"204","parentid":"101","name":"\xe8\xb5\x84\xe9\x87\x91\xe7\xae\xa1\xe7\x90\x86","path":"/invest","children":null},{"id":"209","parentid":"101","name":"\xe8\x8f\x9c\xe5\x8d\x95\xe8\xae\xbe\xe7\xbd\xae","path":"/menu","children":null}]},{"id":"102","parentid":"0","name":"\xe7\x94\xa8\xe6\x88\xb7\xe8\xae\xbe\xe7\xbd\xae","path":"/title2","children":[{"id":"210","parentid":"102","name":"\xe6\x95\xb0\xe6\x8d\xae\xe5\xb1\x95\xe7\xa4\xba","path":"/echarts","children":null},{"id":"211","parentid":"102","name":"\xe7\x94\xa8\xe6\x88\xb7\xe6\xb7\xbb\xe5\x8a\xa0","path":"/user","children":null},{"id":"212","parentid":"102","name":"\xe8\xa7\x92\xe8\x89\xb2\xe6\xb7\xbb\xe5\x8a\xa0","path":"/role","children":null},{"id":"213","parentid":"102","name":"\xe6\x95\xb0\xe6\x8d\xae\xe6\x94\xb6\xe9\x9b\x86","path":"/collect","children":null}]}]}'
    #print()
def test2():
    print('hellow')
    ss= sys.path
    print(ss)
    s=Decimal('1.3343')
    print(s)
def test3():
    print(sys.path)
def test4():
    # step1获取数据(从数据库中获取)
    db = pymysql.Connect('localhost', 'root', 'abcde@124', 'test')
    # 创建游标
    cursor = db.cursor()
    # 要执行的sql
    sql = 'select * from invest_base'
     #print(sql)
    cursor.execute(sql)
    info = cursor.fetchall()
    print(len(info[0]))
    db.commit()
    # 关闭游标
    cursor.close()
    # 关闭数据库
    db.close()
def dbDecimal(self,msg):
        print('dbDecimal',type(msg))
        return Decimal(msg)
def getTime():
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
#获取前1天的时间 "%Y-%m-%d"
def getDate(mytime='today',format='%Y-%m-%d',num=0):
    if (mytime=='today'):
        mytime=datetime.date.today().strftime(format)
    stdtime = datetime.datetime.strptime(mytime, format)
    newtime = (stdtime + datetime.timedelta(days=num)).strftime(format)
    return newtime
def random(num2):
    list2 = []
    for number in range(num2):
        str2 = str(random.randint(0, 9))
        list2.append(str2)
    b = " ".join(list2).replace(" ", "")
    return b
def querystock(code):
    df = ts.get_hist_data(code)
    # 保存备份
    print(df)
    #df.to_csv('d:/data/stock/' + code + '.csv')
#print(getDate('2018-05-18',-10,'%Y-%m-%d'))#%Y-%m-%d %H:%M:%S
#print(getDate('2020-01-21 12:32:16',format='%Y-%m-%d %H:%M:%S'))
#random(4)
def backup():
    path='d:/data/test/'
    if os.path.exists(path):
        shutil.rmtree(path)
    else:
        print('no exists')
def btc(tradedate):
    ts.set_token('412a06b413114c8e56aa4ac2b56a4b76caa08c5d8d216ea845987680')
    pro = ts.pro_api()
    #df = pro.coincap(trade_date=tradedate)
    df = pro.btc_marketcap(start_date='20200321', end_date='20200321')
    print(len(df))

    df.to_csv('d:/data/whitehorse/btc-'+str(tradedate)+'.csv',index=None)


btc(20200321)
#querystock('sz500')